﻿
//获取ajax对象
function getajaxo() {
    var ajax = false;
    try {
        ajax = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            ajax = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (E) {
            ajax = false;
        }
    }
    if (!ajax && typeof XMLHttpRequest != 'undefined') {
        ajax = new XMLHttpRequest();
    }
    return ajax;
}

function validstring(value, minLength, maxLength, baseString, badString) {
    var sString = new String(value);
    //最短要求

    if (minLength > 0) if (sString.length < minLength) return false;
    //最长要求

    if (maxLength > 0) if (sString.length > maxLength) return false;
    var i = 0;
    //基础字符串检查

    if (baseString.length > 0) {
        for (i = 0; i < sString.length; i++)
            if (baseString.indexOf(sString.substr(i, 1)) < 0) return false;
    }
    //非法字符串检查

    for (i = 0; i < badString.length; i++)
        if (sString.indexOf(badString.substr(i, 1)) >= 0) return false;

    return true;

}

function validemail(Email) {
    if (document.layers || document.getElementById || document.all) {
        var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
        if (filter.test(Email))
            return true;
        else
            return false;
    }
    else
        return true;
}

//获取字符长度
function strlen(str) {
    var ii = 0;
    var strlen = str.length;
    for (var i = 0; i < strlen; i++) {
        if (str.charCodeAt(i) > 255) { ii += 2; }
        else { ii++; }
    }
    return ii;
}

function getcookie(name) {
    var cookie_start = document.cookie.indexOf(name);
    var cookie_end = document.cookie.indexOf(";", cookie_start);
    return cookie_start == -1 ? '' : unescape(document.cookie.substring(cookie_start + name.length + 1, (cookie_end > cookie_start ? cookie_end : document.cookie.length)));
}

function setcookie(cookieName, cookieValue, seconds, path, domain, secure) {
    var expires = new Date();
    expires.setTime(expires.getTime() + seconds);
    document.cookie = escape(cookieName) + '=' + escape(cookieValue)
		+ (expires ? '; expires=' + expires.toGMTString() : '')
		+ (path ? '; path=' + path : '/')
		+ (domain ? '; domain=' + domain : '')
		+ (secure ? '; secure' : '');
}

//验证日期
function IsDate(strDate) {
    var reg = /^(\d{4})-(\d{1,2})-(\d{1,2})$/;
    var r = strDate.match(reg);
    if (r == null) {
        return false
    };
    r[2] = r[2] - 1;
    var d = new Date(r[1], r[2], r[3]);
    if (d.getFullYear() != r[1] || d.getMonth() != r[2] || d.getDate() != r[3]) {
        return false;
    }
    return true;
}
function IsChinese(strName) {

    var reg = /^[\u0391-\uFFE5]+$/;
    if (reg.test(strName)) {
        return true;
    }
    else
        return false;
}

function validateLogin(callback,arg1,arg2,arg3,arg4) {
    $.get("/Validate.ashx",
    { "a": "is-login" },
    function(data) {
        var result = eval("(" + data + ")");
        if (result.r == "y") {
            callback ? callback.call(this, arg1, arg2, arg3, arg4) : "";
        } else {
        minlogin(callback, arg1, arg2, arg3, arg4);
        }
    });
}

function minlogin(callback,arg1,arg2,arg3,arg4) {
    if ($("#login-div").length <= 0) {
        $(document.body).append("<div id='login-div' style='display:none;'><iframe ' scrolling='no' frameborder=no src='/Minlogin.aspx' style='border:0;width:370px;height:250px;background-color:#EEEEEE;' /></div>");
    }

    $("#login-div").dialog({
        close: function() {
            if (getcookie("uname") != '') {
                $("#login-div").dialog("destroy");
                $("#login-div").remove();
                callback ? callback.call(this, arg1, arg2, arg3, arg4) : "";
            }
        },
        width: 415,
        height: 320,
        modal: true
    });
}

function destroyMinlogin() {
    $("#login-div").dialog("close");
}

function setLoginInfo(name) {
    var ele = $(window.parent.document.getElementById("header_ctl00_login"));
    if (ele.length > 0 && name && name != "" && '<%=this.Passport.IsLogin.ToString().ToUpper() %>' == "TRUE") {
        ele.html("[您好，<a target='_blank' href='/Member/Account.aspx'>" + name + "</a>！<a href='/Logout.aspx'>退出</a>]");
    }
}

function setCart() {
    var count = getcookie("cartcount");
    if (count != "") {
        $("span[id$=_spCartCount]").each(function() {
            $(this).text(count);
        });
    }
}

function shade(elementID) {
    if ($("#shade-div").length <= 0) {
        $(document.body).append("<div id='shade-div' style='display:none;position: absolute;background-color: Black;top: 0px;margin: 0px;opacity: .3;filter: Alpha(Opacity=30);z-index: 100;'></div>");
    }

    $("#shade-div").height($(document).height());
    $("#shade-div").width($(document).width());
    $("#shade-div").show();

    var element = $("#" + elementID)//.appendTo($("#shade-div"));
    element.show();

    element.css("position", "absolute");
    element.css("top", "0");
    element.height($(document).height());
    element.width($(document).width());
    element.css("z-index", "101");
}

function close_shade(elementID) {
    $("#shade-div").hide();
    if (elementID) {
        $("#" + elementID).hide();
    }
}

function loading() {
    if ($("#loading-div").length <= 0) {
        $(document.body).append("<div id='loading-div' style='display:none;text-align:center;'><img src='/images/loading.gif' id='loadingImg'/></div>");
    }
    shade("loading-div");
    $("#loadingImg").css("margin-top", $(document).scrollTop() + ($(window).height() - $("#loadingImg").height()) / 2);
}

function close_loading() {
    close_shade("loading-div");
}

function search() {
    var search_key = $("#searchtxt").val();
    var url = '/templates.aspx?w=' + escape(search_key) + '';
    location.href = url;
}

/**
* 图片头数据加载就绪事件 - 更快获取图片尺寸
* @version	2011.05.27
* @author	TangBin
* @see		http://www.planeart.cn/?p=1121
* @param	{String}	图片路径
* @param	{Function}	尺寸就绪
* @param	{Function}	加载完毕 (可选)
* @param	{Function}	加载错误 (可选)
* @example imgReady('http://www.google.com.hk/intl/zh-CN/images/logo_cn.png', function () {
alert('size ready: width=' + this.width + '; height=' + this.height);
});
*/
var imgReady = (function() {
    var list = [], intervalId = null,

    // 用来执行队列
	tick = function() {
	    var i = 0;
	    for (; i < list.length; i++) {
	        list[i].end ? list.splice(i--, 1) : list[i]();
	    };
	    !list.length && stop();
	},

    // 停止所有定时器队列
	stop = function() {
	    clearInterval(intervalId);
	    intervalId = null;
	};

    return function(url, ready, load, error) {
        var onready, width, height, newWidth, newHeight,
			img = new Image();

        img.src = url;

        // 如果图片被缓存，则直接返回缓存数据
        if (img.complete) {
            ready.call(img);
            load && load.call(img);
            return;
        };

        width = img.width;
        height = img.height;

        // 加载错误后的事件
        img.onerror = function() {
            error && error.call(img);
            onready.end = true;
            img = img.onload = img.onerror = null;
        };

        // 图片尺寸就绪
        onready = function() {
            newWidth = img.width;
            newHeight = img.height;
            if (newWidth !== width || newHeight !== height ||
            // 如果图片已经在其他地方加载可使用面积检测
				newWidth * newHeight > 1024
			) {
                ready.call(img);
                onready.end = true;
            };
        };
        onready();

        // 完全加载完毕的事件
        img.onload = function() {
            // onload在定时器时间差范围内可能比onready快
            // 这里进行检查并保证onready优先执行
            !onready.end && onready();

            load && load.call(img);

            // IE gif动画会循环执行onload，置空onload即可
            img = img.onload = img.onerror = null;
        };

        // 加入队列中定期执行
        if (!onready.end) {
            list.push(onready);
            // 无论何时只允许出现一个定时器，减少浏览器性能损耗
            if (intervalId === null) intervalId = setInterval(tick, 40);
        };
    };
})();
